<template>
  <div>
    <el-dialog
      :visible="true"
      :title="title"
      custom-class="check_dialog"
      width="50%"
      :before-close="callOf"
    >
      <el-form
        ref="formCheck"
        :model="checkForms"
        :rules="formrules"
        label-width="100px"
      >

        <el-form-item label="项目名称" prop="project_name">
          <el-input
            ref="project_name"
            v-model="checkForms.project_name"
            class="add"
            :disabled="true"
          />
        </el-form-item>

        <el-form-item label="产品经理" prop="pro_manager">
          <el-input
            ref="pro_manager"
            v-model="checkForms.pro_manager"
            class="add"
          />
        </el-form-item>
        <el-form-item label="开发人员" prop="developer">
          <el-input
            ref="developer"
            v-model="checkForms.developer"
            class="add"
          />
        </el-form-item>
        <el-form-item label="QA" prop="qa">
          <el-input ref="qa" v-model="checkForms.qa" class="add" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="begin_time">
              <!-- <el-input
                ref="begin_time"
                v-model="checkForms.begin_time"
                class="add"
              /> -->
              <el-date-picker
                ref="begin_time"
                v-model="checkForms.begin_time"
                type="date"
                placeholder="选择开始日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="end_time">
              <!-- <el-input
                ref="end_time"
                v-model="checkForms.end_time"
                class="add"
              /> -->
              <el-date-picker
                ref="end_time"
                v-model="checkForms.end_time"
                type="date"
                placeholder="选择结束日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="几人.天" prop="several_days">
              <el-input
                ref="several_days"
                v-model="checkForms.several_days"
                class="add"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="bug数量" prop="bug_counts">
              <el-input
                ref="bug_counts"
                v-model="checkForms.bug_counts"
                class="add"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="已修复bug数量" prop="bug_repair">
              <el-input
                ref="bug_repair"
                v-model="checkForms.bug_repair"
                class="add"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="遗留bug数量" prop="bug_legacy">
              <el-input
                ref="bug_legacy"
                v-model="checkForms.bug_legacy"
                class="add"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建议/用户体验bug数量" prop="advice_counts">
              <el-input
                ref="advice_counts"
                v-model="checkForms.advice_counts"
                class="add"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="bug密度" prop="bug_density">
              <el-input
                ref="bug_density"
                v-model="checkForms.bug_density"
                class="add"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="bug引入率" prop="bug_introd">
              <el-input
                ref="bug_introd"
                v-model="checkForms.bug_introd"
                class="add"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="测试结论" prop="conclusion">
              <el-radio
                v-model="checkForms.conclusion"
                label="测试通过"
              >测试通过</el-radio>
              <el-radio
                v-model="checkForms.conclusion"
                label="测试未通过"
              >测试未通过</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="Mantis地址" prop="mantis_adress">

          <!-- <el-link :href="checkForms.mantis_adress" > -->
          <el-input
            ref="mantis_adress"
            v-model="checkForms.mantis_adress"
            class="add"
            type="textarea"
            placeholder="请输入yapi/git地址"
          />
          <!-- </el-link> -->
          <!-- <el-link :href="checkForms.mantis_adress">{{ checkForms.mantis_adress }} </el-link> -->
        </el-form-item>
        <el-form-item label="Mantis访问帐号\密码" prop="mantis_account">
          <el-input
            ref="mantis_account"
            v-model="checkForms.mantis_account"
            class="add"
            :disabled="true"
            default-val="guest\guest"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            ref="remarks"
            v-model="checkForms.remarks"
            class="add"
            placeholder="请输入备注"
            type="textarea"
            default-val="上线后请及时告知，以便线上走查。"
          />
        </el-form-item>
        <el-form-item label="主送" prop="addressee">
          <el-select
            v-model="checkForms.addressee"
            class="add"
            multiple
            filterable
            placeholder="请选择主送人（支持模糊搜索）"
            style="width: 100%"
          >
            <el-option
              v-for="item in mainOptions"
              :key="item.username"
              :label="`${item.username}-${item.email}`"
              :value="item.email"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="抄送" prop="cc">
          <el-select
            v-model="checkForms.cc"
            class="add"
            multiple
            filterable
            placeholder="请选择抄送人（支持模糊搜索）"
            style="width: 100%"
          >
            <el-option
              v-for="item in ccOptions"
              :key="item.username"
              :label="`${item.username}-${item.email}`"
              :value="item.email"
            />
          </el-select>
        </el-form-item>
        <div />
        <el-form-item>
          <el-button
            type="primary"
            @click="dialogFormCheck('formCheck')"
          >保存</el-button>
          <el-button
            type="primary"
            @click="callOf('formCheck')"
          >取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { Message } from 'element-ui'
import { updateTestReport } from '@/api/quality'
import axios from 'axios'

export default {
  name: 'Task',
  props: {
    // dialogAdd: Object,
    checkForms: {
      type: Object,
      required: true
    },
    edit: Boolean,
    title: {
      type: String,
      required: true
    }
  },

  data() {
    var valiNumberPass1 = (rule, value, callback) => {
      // 包含小数的数字
      const reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
      if (value === '') {
        callback(new Error('不可为空'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入数字（最多包含一位小数）'))
        if (value.split('.').length > 1) {
          callback(new Error('最多保留一位小数'))
        }
      } else if (value === '0') {
        callback(new Error('请输入大于0的数字（最多包含一位小数）'))
        if (value.split('.').length > 1) {
          callback(new Error('最多保留一位小数'))
        }
      } else {
        callback()
      }
    }
    var valiNumberPass2 = (rule, value, callback) => {
      // 包含小数的数字
      const reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g
      if (value === '') {
        callback(new Error('不可为空'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入数字（最多包含两位小数）'))
        if (value.split('.').length > 2) {
          callback(new Error('最多保留两位小数'))
        }
      } else {
        callback()
      }
    }
    var valiNumberPass = (rule, value, callback) => {
      // 整数
      const reg = /^[0-9]([0-9])*$/
      if (value === '') {
        callback(new Error('不可为空'))
      } else if (!reg.test(value)) {
        callback(new Error('请输入整数数字'))
      } else {
        callback()
      }
    }

    var valiNumberPassTime = (rule, value, callback) => {
      // 整数
      if (value === '') {
        callback(new Error('不可为空'))
      } else {
        callback()
      }
    }
    return {
      conclusion: '0',
      projectOptions: [],
      mainOptions: [],
      ccOptions: [],
      formrules: {
        project_name: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' }
        ],
        begin_time: [
          { required: true, validator: valiNumberPassTime, trigger: 'blur' }
        ],
        end_time: [
          { required: true, validator: valiNumberPassTime, trigger: 'blur' }
        ],
        several_days: [
          { required: true, validator: valiNumberPass1, trigger: 'blur' }
        ],
        pro_manager: [
          { required: true, message: '产品经理不能为空', trigger: 'blur' }
        ],
        developer: [
          { required: true, message: '开发人员不能为空', trigger: 'blur' }
        ],
        qa: [{ required: true, message: 'QA不能为空', trigger: 'blur' }],
        bug_counts: [
          { required: true, validator: valiNumberPass, trigger: 'blur' }
        ],
        bug_repair: [
          { required: true, validator: valiNumberPass, trigger: 'blur' }
        ],
        bug_legacy: [
          { required: true, validator: valiNumberPass, trigger: 'blur' }
        ],
        advice_counts: [
          {
            required: true,
            validator: valiNumberPass,
            trigger: 'blur'
          }
        ],
        bug_density: [
          { required: true, validator: valiNumberPass2, trigger: 'blur' }
        ],
        bug_introd: [
          { required: true, validator: valiNumberPass2, trigger: 'blur' }
        ],
        conclusion: [
          { required: true, message: '测试结论不能为空', trigger: 'blur' }
        ],
        mantis_adress: [
          { required: true, message: 'mantis地址不能为空', trigger: 'blur' }
        ],
        mantis_account: [
          {
            required: true,
            message: 'Mantis访问帐号密码不能为空',
            trigger: 'blur'
          }
        ],
        addressee: [
          { required: true, message: '主送不能为空', trigger: 'blur' }
        ],
        cc: [
          { required: true, message: '抄送不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    axios
      .get('http://connection.360che.com/server/admin.php/ApiData/UserInfo')
      .then((response) => {
        this.mainOptions = response.data.data
        this.ccOptions = response.data.data
        this.mainOptions.push({ 'email': 'qa-group@360che.com', 'username': '项目测试部' }, { 'email': 'app-gp@360che.com', 'username': 'APP邮件组' },
          { 'email': 'bigdata-gp@360che.com', 'username': '大数据邮件组' }, { 'email': 'shangxian-gp@360che.com', 'username': '项目上线通知' })
      }
      )
  },
  methods: {
    formateDate(dateformate) {
      var date = new Date(dateformate)
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      var d = date.getDate()
      // var h = date.getHours();
      // var mi = date.getMinutes();
      m = m > 9 ? m : '0' + m
      // alert("y:"+y+"m:"+m+"d:"+d+"h:"+h+"mi:"+mi);
      return y + '-' + m + '-' + d
    },
    dialogFormCheck(formCheck) {
      this.$refs[formCheck].validate((valid) => {
        if (valid) {
          if (
            new Date(this.checkForms.end_time) <
            new Date(this.checkForms.begin_time)
          ) {
            Message.error('结束时间不能小于开始时间!')
            return
          }
          this.checkForms.begin_time = this.formateDate(this.checkForms.begin_time)
          this.checkForms.end_time = this.formateDate(this.checkForms.end_time)
          updateTestReport(this.checkForms)
            .then((res) => {
              Message.success(res.message)
              this.callOf()
              this.$parent.getList()
            })
            .catch((err) => {
              Message.error(err.msg || err.message || err.value)
            })
        } else {
          Message.error('请检查验收报告内容必填项！')
        }
      })
    },
    callOf() {
      this.$emit('closePop')
    },
    winOpen() {
      window.open(this.checkForms.mantis_adress)
    }
  }
}
</script>
<style scoped>
/*.add {
  width: 100%;
}*/
/deep/.check_dialog .el-dialog__body {
  max-height: 600px;
  overflow: auto;
}
</style>
